package tgc.edu.library.web.controller;

import java.util.HashSet;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import tgc.edu.library.custom.CommonController;
import tgc.edu.library.custom.EasyuiResult;
import tgc.edu.library.custom.LogUtils;
import tgc.edu.library.entity.Librarian;
import tgc.edu.library.entity.SysRole;
import tgc.edu.library.entity.SysUser;
import tgc.edu.library.security.UserUtils;
import tgc.edu.library.service.LibrarianService;
import tgc.edu.library.service.SysRoleService;
import tgc.edu.library.web.form.LibrarianForm;

@Controller
@RequestMapping(value="/librarian")
public class LibrarianController extends CommonController<Librarian, Integer,LibrarianForm>{
	@Autowired
	private SysRoleService sysroleService;
	@Autowired
	private LibrarianService librarianService;
	@Autowired
	private LogUtils logUtils;
	@Autowired
	private UserUtils userUtils;
	
	@Override
	public Specification<Librarian> buildSpec(LibrarianForm form) {
		Specification<Librarian> specification=new Specification<Librarian>() {

			private static final long serialVersionUID = 1L;

			@Override
			public Predicate toPredicate(Root<Librarian> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				HashSet<Predicate> rules=new HashSet<>();
				String search = form.getSearch();
				if(StringUtils.hasText(search)) {
					Predicate username = cb.like(root.get("username"), "%"+search+"%");
					Predicate name = cb.like(root.get("name"), "%"+search+"%");
					Predicate number= cb.like(root.get("number"), "%"+search+"%");
					Predicate idCard = cb.like(root.get("idCard"), "%"+search+"%");
					Predicate phone = cb.like(root.get("phone"), "%"+search+"%");
					Predicate address = cb.like(root.get("address"), "%"+search+"%");
					Predicate or = cb.or(username,name,number,idCard, phone,address);
					rules.add(or);
				}
				return cb.and(rules.toArray(new Predicate[rules.size()]));
			}
		};
		return specification;
	}

	@RequestMapping(value="/save")
	public Object save(LibrarianForm form) {
		SysUser user = userUtils.getUser();
		Librarian model = new Librarian();
		SysRole role = sysroleService.findByCode("ROLE_BOOKADMIN");
		Integer id = form.getId();
		if(id!=null) {
			model = librarianService.findById(id);
		}
		model.setUsername(form.getUsername());
		model.setPassword(new BCryptPasswordEncoder().encode("123"));
		model.setName(form.getName());
		model.setNumber(form.getNumber());
		model.setIdCard(form.getIdCard());
		model.setPhone(form.getPhone());
		model.setAddress(form.getAddress());
		model.getRole().add(role);
		logUtils.log(user.getUsername(), "添加管理员");
		librarianService.save(model);
		return new EasyuiResult("数据保存成功");
	}
}
